using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fieldbook.Entities;
using Fieldbook.Business;

namespace Fieldbook.Testwebsite.User
{
    public partial class FieldMaps : FieldbookPage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
        }

        protected void dgvFieldMaps_SelectedIndexChanged(object sender, EventArgs e)
        {
            int fieldMapID = Int32.Parse(dgvFieldMaps.SelectedValue.ToString());
            FieldMap fMap = FieldController.GetFieldMap(fieldMapID);
            this.imgFieldMap.ImageUrl = String.Format("~/FieldMaps/{0}.jpg", fMap.MapFileName);
            this.imgFieldMap.Visible = true;
        }

        protected void linkAddMap_Command(object sender, CommandEventArgs e)
        {
            Guid fileName = Guid.NewGuid();
            string newImageName = String.Format("~/FieldMaps/{0}.jpg", fileName);
            try
            {
                FileUpload1.SaveAs(MapPath(newImageName));
                FieldMap map = new FieldMap();
                map.PostalCode = this.txtPostalCode.Text;
                map.Description = this.txtDescription.Text;
                map.MapFileName = fileName;
                FieldController.InsertFieldMap(map);
                lblOperationStatus.Text = "Map added sucessfully";

                this.txtPostalCode.Text = "";
                this.txtDescription.Text = "";
                this.dgvFieldMaps.DataBind();
            }
            catch 
            {
                lblOperationStatus.Text = "Error adding map";
            }
        }
    }
}
